﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.WebApiTool
{
    public class PageIndexBuilder
    {
        StringBuilder _stringBuilder = new StringBuilder();

        public void Begin()
        {
            _stringBuilder.Append("<!DOCTYPE html>");
            _stringBuilder.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            _stringBuilder.Append("<head>");
            _stringBuilder.Append("<title></title>");
            _stringBuilder.Append("<link href=\"./css/style.css\" rel=\"stylesheet\" />");
            _stringBuilder.Append("</head>");

            _stringBuilder.Append("<body>");

            _stringBuilder.Append("<div style=\"position: fixed; left: 0; top: 0; bottom: 0; width: 250px; overflow-y:scroll\">");

            _stringBuilder.Append("<ul>");
        }

        public void End()
        {
            _stringBuilder.Append("</div>");
            _stringBuilder.Append("<div style=\"position: fixed; left: 250px; top: 0; bottom: 0; right: 0;\">");
            _stringBuilder.Append("<iframe name=\"contentFrame\" id=\"contentFrame\" style=\"width:100%; height:100%;\"></iframe>");
            _stringBuilder.Append("</div>");
            _stringBuilder.Append("</body>");
            _stringBuilder.Append("</html>");
        }

        public void Build(DScenario scenario)
        {
            _stringBuilder.Append("<li>");
            _stringBuilder.Append(scenario.Name);
            _stringBuilder.Append("<ul>");

            foreach (var action in scenario.Actions)
            {
                _stringBuilder.Append("<li>");
                _stringBuilder.AppendFormat("<a href='./Docs/{0}_{1}.html'  target=\"contentFrame\">", scenario.Name, action.Name);
                _stringBuilder.AppendFormat("{0}_{1}", scenario.Name, action.Name);
                _stringBuilder.Append("</a>");
                _stringBuilder.Append("</li>");
            }

            _stringBuilder.Append("</ul>");

            _stringBuilder.Append("</li>");
        }

        public void Save(string filePath)
        {
            File.WriteAllText(filePath, _stringBuilder.ToString());
        }
    }
}
